package data;

import java.util.*;

/**
 * @author Yakiv Tymoshenko
 * @since 14.10.12
 */
public class Table {

    private List<String[]> data;

    public Table() {
        this(new ArrayList<String[]>());
    }

    public Table(List<String[]> data) {
        this.data = data;
    }

    public void addRow(String row) {
        StringTokenizer tokenizer = new StringTokenizer(row, ", ");
        String index, key, left, right;
        index = tokenizer.nextToken();
        key = tokenizer.nextToken();
        left = tokenizer.nextToken();
        right = tokenizer.nextToken();
        addRow(index, key, left, right);
    }

    public void addRow(String index, String key, String left, String right) {
        if (left == null) {
            left = "NIL";
        }
        if (right == null) {
            right = "NIL";
        }
        data.add(new String[] {index, key, left, right});
    }

    public List<String[]> getData() {
        return new ArrayList<String[]>(data);
    }

    public Set<Object> getKeySet() {
        Set<Object> result = new LinkedHashSet<Object>(data.size());
        for(String[] row : data) {
            result.add(row[1]);
        }
        return result;
    }
}
